package com.itheima.freemarker.controller;

import com.itheima.freemarker.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

//@RestController
@Controller //用这个注解
public class HelloController {

    @GetMapping("basic")
    public String hello(Model model){
        model.addAttribute("name", null);

        Student student = new Student();
        student.setName("张三");
        student.setAge("18");
        model.addAttribute("stu", student);

        return "00-basic";  //  请求转发   templates/00-basic.ftl
    }



    @GetMapping("list")
    public String list(Model model){

        List<Student> list = new ArrayList<>();

        Student s1 = new Student();
        s1.setName("张三");
        s1.setAge("18");
        s1.setMoney(100f);

        Student s2 = new Student();
        s2.setName("李四");
        s2.setAge("19");
        s2.setMoney(200f);

        Student s3 = new Student();
        s3.setName("王五");
        s3.setAge("20");
        s3.setMoney(300f);

        list.add(s1);
        list.add(s2);
        list.add(s3);

        model.addAttribute("stus", list);

        return "01-list";  //  请求转发   templates/01-list.ftl
    }






    @GetMapping("map")
    public String map(Model model){
        Student s1 = new Student();
        s1.setName("张三");
        s1.setAge("18");
        s1.setMoney(100f);
        Student s2 = new Student();
        s2.setName("李四");
        s2.setAge("19");
        s2.setMoney(200f);

        Map<String,Object> map = new HashMap<>();
        map.put("stu1", s1);
        map.put("stu2", s2);

        model.addAttribute("stuMap", map);




        return "02-map";  //  请求转发   templates/02-map.ftl
    }





    @GetMapping("if")
    public String testif(Model model){

        List<Student> list = new ArrayList<>();

        Student s1 = new Student();
        s1.setName("小红");
        s1.setAge("18");
        s1.setMoney(100f);

        Student s2 = new Student();
        s2.setName("小蓝");
        s2.setAge("19");
        s2.setMoney(200f);

        Student s3 = new Student();
        s3.setName("王五");
        s3.setAge("20");
        s3.setMoney(300f);

        list.add(s1);
        list.add(s2);
        list.add(s3);

        model.addAttribute("stus", list);

        return "03-if";  //  请求转发   templates/03-if.ftl
    }



    @GetMapping("yunsuan")
    public String yunsuan(Model model){


        return "04-yunsuan";  //  请求转发   templates/03-if.ftl
    }




    @GetMapping("fun")
    public String fun(Model model){

        //1.1 小强对象模型数据
        Student stu1 = new Student();
        stu1.setName("小强");
        stu1.setAge("18");
        stu1.setMoney(1000.86f);
        stu1.setBirthday(new Date());
        //1.2 小红对象模型数据
        Student stu2 = new Student();
        stu2.setName("小红");
        stu2.setMoney(200.1f);
        stu2.setAge("19");
        //1.3 将两个对象模型数据存放到List集合中
        List<Student> stus = new ArrayList<>();
        stus.add(stu1);
        stus.add(stu2);
        model.addAttribute("stus", stus);
        // 2.1 添加日期
        Date date = new Date();
        model.addAttribute("today", date);
        // 3.1 添加数值
        model.addAttribute("point", 102920122);




        return "05-fun";
    }

}
